package com.glwan.fixedassetapp.Adapter;

import static com.glwan.fixedassetapp.common.ExcelTemplateConstant.getDatabaseKeyMapping;
import static com.glwan.fixedassetapp.common.ExcelTemplateConstant.getDatabaseKeyMappingNew;

import android.content.Context;
import android.graphics.Color;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.glwan.fixedassetapp.R;
import com.glwan.fixedassetapp.model.Asset;
import com.glwan.fixedassetapp.model.AssetOld;

import org.apache.commons.lang.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

public class OfflineAdapter extends BaseAdapter {
    private final Context mContext;
    private final List<Asset> assetList = new ArrayList<>();

    public OfflineAdapter(Context context) {
        this.mContext = context;
    }

    public void setAssetList(List<Asset> assetList) {
        this.assetList.clear();
        this.assetList.addAll(assetList);
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return assetList.size();
    }

    @Override
    public Asset getItem(int i) {
        return assetList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_asset_info, parent, false);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        Asset productInfo = getItem(position);

        if (productInfo != null) {
            viewHolder.bind(productInfo);
        }
        return convertView;
    }

    private static String getAttributeValue(Asset asset, String attributeName) {
        try {
            String databaseKey = attributeName.substring(0, 1).toLowerCase() + attributeName.substring(1);
            // 使用反射获取属性值
            java.lang.reflect.Field field = Asset.class.getDeclaredField(databaseKey);
            field.setAccessible(true);
            return (String) field.get(asset);
        } catch (Exception e) {
            return null;
        }
    }

    static class ViewHolder {
        private TextView tvTaskID;
        private TextView tvStatus;

        ViewHolder(View itemView) {
            tvTaskID = itemView.findViewById(R.id.tvTaskID);
            tvStatus = itemView.findViewById(R.id.tvStatus);

        }

        void bind(Asset info) {

            // 获取数据库键映射的 map
            Map<String, String> databaseKeyMapping = getDatabaseKeyMappingNew();

            // 创建一个 StringBuilder 用于拼接字符串
            StringBuilder stringBuilder = new StringBuilder();
            if (!TextUtils.isEmpty(info.getTaskId())) {
                stringBuilder.append("TaskID").append(": ").append(info.getTaskId()).append("\n");
            }
            // 遍历 Asset 对象的属性
            for (Map.Entry<String, String> entry : databaseKeyMapping.entrySet()) {
                // 获取属性名
                String attributeName = entry.getKey();
                // 获取 map 中与属性名对应的键值对
                String databaseKey = entry.getValue();
                // 根据属性名使用反射获取属性值
                String attributeValue = getAttributeValue(info, databaseKey);
                // 如果属性值不为空，则拼接到 StringBuilder 中
                if (attributeValue != null) {
                    if(!attributeName.equals("盘点状态")) {
                        stringBuilder.append(attributeName).append(": ").append(attributeValue).append("\n");
                    }
                }
            }
            tvTaskID.setText(stringBuilder.toString());
            // 原始字符串
            String inventoryStatus = info.getInventoryStatus();

            if (!TextUtils.isEmpty(inventoryStatus)) {
                SpannableString spannableString = new SpannableString("盘点状态：" + inventoryStatus);
                int startIndex = spannableString.toString().indexOf(inventoryStatus);
                int endIndex = startIndex + inventoryStatus.length();
                ForegroundColorSpan colorSpan = new ForegroundColorSpan(Color.RED);
                spannableString.setSpan(colorSpan, startIndex, endIndex, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                tvStatus.setText(spannableString);
                tvStatus.setVisibility(View.VISIBLE);
            } else {
                tvStatus.setVisibility(View.GONE);
            }
        }
    }
}
